package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Intervention;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.InterventionService;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/ordersetting")
@RestController
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @Reference
    private OrderService orderService;

    @Reference
    private InterventionService interventionService;

    //接收前端传来的excel表格文件，并且解析其中数据，存入数据库

    @PreAuthorize("hasAuthority('ORDERSETTING')")//权限校验
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile) {
        try {
            //读取Excel文件数据，封装到集合中
            List<String[]> list = POIUtils.readExcel(excelFile);
            //将读取的数据封装到OrderSetting对象中，方便存储于数据库的表中
            if (list != null && list.size() > 0) {
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
                //调用业务层添加方法
                orderSettingService.add(orderSettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    //根据月份查询当月预约信息
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//参数格式为：2019-03
        try{
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
           // System.out.println("------"+list);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,list);

        }catch (Exception e){
            e.printStackTrace(); //获取预约设置数据失败 return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('ORDERSETTING')")//权限校验
    // 根据指定日期修改可预约人数
    @RequestMapping("/editOrderSettingByDate")
    public Result editOrderSettingByDate(@RequestBody OrderSetting orderSetting){
        try{orderSettingService.editNumberByDate(orderSetting);
            //预约设置成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //预约设置失败
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }


    //查询所有预约信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult=null;
        try {
            pageResult= orderService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    //更新就诊状态
    @RequestMapping("/statusEdit")
    public Result statusEdit(@RequestBody Order order) {
        try {
            orderService.statusEdit(order);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    //根据预约订单id查询膳食干预id
    @RequestMapping("/findFoodIdsByOrderId")
    public Result findFoodIdsByOrderId(Integer orderId){
        List<Integer> foodIds = interventionService.findFoodIdsByOrderId(orderId);
        return null;
    }


    //根据预约订单id添加医生建议
    @RequestMapping("/edit")
    public Result edit(@RequestBody Intervention intervention, Integer[] foodIds, Integer[] sportIds){
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        System.out.println(username);
        System.out.println(foodIds);
        System.out.println(sportIds);
        System.out.println(intervention);
        intervention.setDoctorName(username);
        try {
            interventionService.addAdvice(intervention,foodIds,sportIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_INTERVENTION_FAIL);
        }
        return new Result(true,MessageConstant.ADD_INTERVENTION_SUCCESS);
    }


}
